
<template>
  <div>
    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}

      </el-checkbox>

    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="setRoles">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    roleId: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      roleIds: [], //  选着的角色
      roleList: [] // 所有角色
    }
  },
  created() {
    // 获取角色方法
    this.getRoles()
    // 获取已有角色全部信息方法
    this.getUserDetailById()
  },
  methods: {
    // 确认 分配角色
    async  setRoles() {
      const res = await assignRoles({ id: this.roleId, roleIds: this.roleIds })
      this.$message.success(res.message)
      this.$emit('close')
    },
    // 获取角色全部信息
    async getUserDetailById() {
      const res = await getUserDetailById(this.roleId)
      console.log(res)
      this.roleIds = res.data.roleIds === null ? [] : res.data.roleIds
    },

    // 获取角色
    async getRoles() {
      const res = await getRoles()
      this.roleList = res.data.rows
    },

    // 取消
    cancleDialog() {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.footer{
  margin-top: 20px;
}
</style>>

